Java Native Access
part 5/7 · 11.9 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
The following program loads the C POSIX library and uses it to call the standard mkdir function.
Note: The following code is portable and works the same on POSIX standards platforms.
import com.sun.jna.Library;
import com.sun.jna.Native;
/** Simple example of native C POSIX library declaration and usage. */
public class ExampleOfPOSIX {
public interface POSIX extends Library {
public int chmod(String filename, int mode);
public int chown(String filename, int user, int group);
public int rename(String oldpath, String newpath);
public int kill(int pid, int signal);
public int link(String oldpath, String newpath);
public int mkdir(String path, int mode);
public int rmdir(String path);
}
public static void main(String[] args) {
// It is possible to load msvcrt for its partial POSIX support on Windows...
POSIX posix = (POSIX) Native.loadLibrary("c", POSIX.class);
// but it will still fail on Windows due to /tmp being missing.
posix.mkdir("/tmp/newdir", 0777);
posix.rename("/tmp/newdir","/tmp/renamedir");
}
}
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────